﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class MenuInformationService : BaseService<MenuInformation>
    {
        public List<CategoryPercentage> categories()
        {
            List<CategoryPercentage> categories = new List<CategoryPercentage>();
            using (var db = new CrookedShowEntities())
            {
                var relus = from a in db.Category
                            join b in db.Product_Spu
                            on a.Id equals b.Two_Category_Id.Value into aa
                            from ab in aa.DefaultIfEmpty()
                            join c in db.Order_Detail
                            on ab.Id equals c.Product_Id into cc
                            from bc in cc.DefaultIfEmpty()
                            where a.ParentId != 0
                            group new { a, ab, bc } by new { a.Id, a.Name, a.ParentId, ab.One_Category_Id } into e
                            select new CategoryPercentage()
                            {
                                Id = e.Key.Id,
                                Name = e.Key.Name,
                                ParentId = e.Key.ParentId,
                                Count = e.FirstOrDefault().ab == null ? 0 : db.Order_Detail.Count(r => r.Order_Detail_Id == e.Key.One_Category_Id)
                            };
                categories = relus.ToList();
                return relus.ToList();
            }
        }
    }
}
